<?php
/**
 * 对UC用户信息获取接口的简单二次封装
 * 
 * @author wanggang
 * 
 */

class OpUser
{
    protected static $_userAttrs = array(
        USERINFO_NICKNAME,
        USERINFO_AVATAR_30,   
        USERINFO_AVATAR_45, 
        USERINFO_AVATAR_90,
    );
    
    /**
     * 获取用户信息
     * 
     * @param integer $uid 用户id
     * @return array
     */
    public static function getUserInfo($uid)
    {
        $user = UCModUserInfo::getUserInfo($uid, self::$_userAttrs);
        if ($user) {
            self::_fixUserInfo($uid, $user);
        }
        return $user;
    }
    
    /**
     * 批量获取用户信息
     * 
     * @param array(integer) $uids
     * @return array uid为键值
     */
    public static function getUserInfos($uids)
    {
        $uids = array_unique($uids);
        $users = UCModUserInfo::getUserInfos($uids, self::$_userAttrs);
        if ($users) {
            $uids = array_keys($users);
            foreach ($uids as $uid) {
                if ($users[$uid]) {
                    self::_fixUserInfo($uid, $users[$uid]);
                }
            }
        }
        return $users;
    }
    
    /**
     * 获取好友id
     * 
     * @param integer $uid
     * @return array
     */
    public static function getFriendUids($uid, $bothFollow = FALSE)
    {
        $uids = array();
        
        $relation = new OpModRelation($uid);
        
        if ($bothFollow) {
            $list = $relation->getBiRelation(0, 2000);
        } else {
            $list = $relation->getFollowsDistinct(0, 2000);
        }
        
        $uids = array();
        
        if ($list && $list['entry']) {
            foreach ($list['entry'] as $item) {
                $uids[] = $item['fsdid'];
            }
        }
        
        return $uids;
    }
    
    /**
     * 将用户信息的结构更改为对当前应用适当的结构
     * 
     * @param array $user in/out parameter
     */
    private static function _fixUserInfo($uid, &$user)
    {
        $fixed = array();
        $fixed['uid'] = $uid;
        $fixed['name'] = $user[USERINFO_NICKNAME];
        if (empty($fixed['name'])) {
            $fixed['name'] = ''.$fixed['uid'];
        }
        $fixed['avatar'] = $user[USERINFO_AVATAR_45];
        $fixed['avatar_big'] = $user[USERINFO_AVATAR_90];
        $fixed['avatar_small'] = $user[USERINFO_AVATAR_30];
        $fixed['home'] = get_user_home_url($uid);
        $user = $fixed;
    }
}